package com.cs797.project;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.View;

public class HotSpotBSListActivity extends ListActivity {

    private static final String TAG = "HotSpotBSListActivity";

    HotSpotDataAdapter hotSpotAdapter;
    Context thisContext;
    int phoneType;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        thisContext = this.getBaseContext();

        Bundle params = this.getIntent().getExtras();
        phoneType = params.getInt("phoneType");

        hotSpotAdapter = new HotSpotDataAdapter(thisContext);
        hotSpotAdapter.open();

        Cursor c = hotSpotAdapter.getBSList();
        List<String> bsInfo = new ArrayList<String>(c.getCount());

        if (c.moveToFirst()) {
            DecimalFormat df = new DecimalFormat("#.######");
            do {
                bsInfo.add(c.getString(0) + "," + c.getString(1) + "," + df.format(c.getDouble(2)) + ","
                        + df.format(c.getDouble(3)));
            } while (c.moveToNext());
        }

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, bsInfo));

        ListView lv = getListView();
        lv.setTextFilterEnabled(false);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedText = ((TextView) view).getText().toString();
                String[] bsInfo = selectedText.split(",");

                if (bsInfo.length != 4) {
                    Toast.makeText(getApplicationContext(), "Unable to parse text: " + selectedText, Toast.LENGTH_SHORT)
                            .show();
                    return;
                }

                Cursor c = hotSpotAdapter.getSignalDataForBS(bsInfo[0], bsInfo[1], Integer.parseInt(bsInfo[2]),
                        Integer.parseInt(bsInfo[3]), phoneType);
                
                double towerLat = hotSpotAdapter.getBSLat(bsInfo[0], bsInfo[1], Integer.parseInt(bsInfo[2]), Integer.parseInt(bsInfo[3]));
                double towerLon = hotSpotAdapter.getBSLon(bsInfo[0], bsInfo[1], Integer.parseInt(bsInfo[2]), Integer.parseInt(bsInfo[3]));

                if (c != null) {
                    startManagingCursor(c);

                    int msNum = 1;
                    Bundle bundle = new Bundle(4 + (c.getCount() * 3));
                    bundle.putBoolean("current_position_only", false);
                    bundle.putInt("numDataPoints", c.getCount());
                    bundle.putDouble("towerLatitude", towerLat);
                    bundle.putDouble("towerLongitude", towerLon);

                    if (c.moveToFirst()) {
                        do {
                            Log.d(TAG, "Got row:");
                            Log.d(TAG, "\tLat: " + c.getDouble(c.getColumnIndex("ms_latitude")));
                            Log.d(TAG, "\tLon: " + c.getDouble(c.getColumnIndex("ms_longitude")));
                            Log.d(TAG, "\tSig: " + c.getDouble(c.getColumnIndex("ms_signal_strength_dbm")));

                            bundle.putDouble("msLatitude" + String.valueOf(msNum),
                                    c.getDouble(c.getColumnIndex("ms_latitude")));
                            bundle.putDouble("msLongitude" + String.valueOf(msNum),
                                    c.getDouble(c.getColumnIndex("ms_longitude")));
                            bundle.putInt("msSignalStrength" + String.valueOf(msNum),
                                    c.getInt(c.getColumnIndex("ms_signal_strength_dbm")));

                            msNum++;
                        } while (c.moveToNext());
                    }

                    Intent mapViewIntent = new Intent(thisContext, HotSpotMapView.class);
                    mapViewIntent.putExtras(bundle);
                    startActivity(mapViewIntent);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (hotSpotAdapter != null) {
            hotSpotAdapter.close();
        }
    }
}
